package com.tzkyzj.tiremanagersystem;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class MyApplication extends Application {

    private static MyApplication instance;
    private SharedPreferences sharedPreferences;
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        sharedPreferences = getSharedPreferences("user_prefs", MODE_PRIVATE);
        context = getApplicationContext();

        Log.d("MyApplication", "Application 初始化完成");
    }

    public static MyApplication getInstance() {
        return instance;
    }

    public boolean isUserLoggedIn() {
        String token = sharedPreferences.getString("token", null);
        return token != null && !token.isEmpty();
    }

    public void clearUserData() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.apply();
    }

    public static Context getAppContext() {
        // 确保 context 不为 null
        if (context == null && instance != null) {
            context = instance.getApplicationContext();
        }
        return context;
    }
}